using System;
using System.Collections.Generic;
using System.Text;
using Zsa.Emulation.Memory;
using System.IO;
using Zsa.Emulation.Cartridge;

namespace Zsa.Emulation.Mappers
{
    /// <summary>
    /// Memory bank for a mapper.
    /// </summary>
    public class MapperMemoryBank : MemoryBankStream
    {
        #region Fields
        /// <summary>
        /// The mapper.
        /// </summary>
        private Mapper _Mapper;

        /// <summary>
        /// The rom bank type.
        /// </summary>
        private RomBankType _BankType;
        #endregion Fields

        #region Constructor
        /// <summary>
        /// Initializes a new instance of the <see cref="T:MemoryBankStream"/> class.
        /// </summary>
        /// <param name="stream">The stream.</param>
        /// <param name="name">The name.</param>
        internal MapperMemoryBank(Mapper mapper,
            MemoryStream stream, RomBankType bank)
            : this(mapper, stream, bank, 0)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="T:MemoryBankStream"/> class.
        /// </summary>
        /// <param name="stream">The stream.</param>
        /// <param name="name">The name.</param>
        internal MapperMemoryBank(Mapper mapper, MemoryStream stream, RomBankType bank, int offset)
            : base(stream, bank.ToString(), offset)
        {
            _Mapper = mapper;
            _BankType = bank;
        }

        #endregion Constructor

        #region Methods
        /// <summary>
        /// Writes the specified address.
        /// </summary>
        /// <param name="address">The address.</param>
        /// <param name="romByte">The rom byte.</param>
        public override void Write(int address, byte romByte)
        {
            if (_Mapper.OnBankWrite(_BankType, (ushort)address, romByte))
            {
                return;
            }
            base.Write(address, romByte);
        }
        #endregion Methods
    }
}
